#!/bin/bash


# 检查是否提供了三个参数
if [ "$#" -ne 3 ]; then
    echo "用法: $0 <yaml路径> <字段名> <新字符串>"
    exit 1
fi

# 获取输入参数
yaml_file="$1"
field_name="$2"
new_value="$3"

# 检查 yq 是否安装
if ! command -v yq &> /dev/null; then
    echo "错误: 'yq' 命令未找到，请先安装 yq 工具。"
    echo "sudo snap install yq"
    exit 1
fi

# 判断变量是否是字符串

num="$3"
if [[ "$num" =~ ^-?[0-9]+([.][0-9]+)?$ ]]; then
	strT=".$field_name = $new_value"
	#echo "number"
elif [[ "$num" == "true" || "$num" == "false" ]]; then
	strT=".$field_name = $new_value"
	#echo "bool"
else
	strT=".$field_name = \"$new_value\""
	#echo "String"
fi


#echo "$strT"

#yq eval --inplace ".$field_name = \"$new_value\"" "$yaml_file"
yq eval --inplace "$strT" "$yaml_file"



# 检查 yq 命令是否执行成功
if [ $? -eq 0 ]; then
    echo "已成功将字段 '$field_name' 更新为 $new_value "
else
    echo "Error: Failed to update $field_name in $yaml_file"
    exit 1
fi

exit 0
